﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HP.Cachalote.WPF.Model;
using HP.Cachalote.WPF.ViewModel;
using HP.Cachalote.UnitTests.FakeServices;
using NUnit.Framework;
using System.Windows;
using System.Windows.Input;

namespace HP.Cachalote.UnitTests.WPFTests
{
    public class TodoLabelTreeViewItemViewModelTest
    {
        IDataService fakeDataService;
        TodoLabelTreeViewModel _todoLabelTreeViewModel;
        TodoLabelTreeViewItemViewModel _tdItem;

        [SetUp]
        public void Setup()
        {
            fakeDataService = new FakeDataService();
            _todoLabelTreeViewModel = new TodoLabelTreeViewModel(fakeDataService);
            _tdItem = _todoLabelTreeViewModel.LabelList.OrderBy((i) => i.OrderIndex).ToList()[0];
        }

        [Test]
        public void TestList()
        {
            var list = _tdItem.SubLabels;
            var list2 = _tdItem.PeerLabels;

            Assert.AreEqual(2, list.Count);
            Assert.AreEqual(2, list2.Count);
        }

        [Test]
        public void TestEdit()
        {
            var cmd = _tdItem.EditCommand;

            if (cmd.CanExecute(_tdItem))
            {
                cmd.Execute(_tdItem);
            }

            Assert.AreEqual(_tdItem.TodoLabelNameVisibility, Visibility.Hidden);
            Assert.AreEqual(_tdItem.TodoLabelNameInputVisibility, Visibility.Visible);
            _tdItem.Name = _tdItem.Name + " (New)";

            var cmdExit = _tdItem.TxtNameOnKeyDownCommand;

            //if (cmdExit.CanExecute(_tdItem))
            //{
            //    cmdExit.Execute(new KeyEventArgs( Keyboard.PrimaryDevice,PresentationSource.CurrentSources,9, Key.Return));
            //}
        }

        [Test]
        public void TestTextBoxNameLostFocusCommand()
        {
            var cmd = _tdItem.TextBoxNameLostFocusCommand;
            _tdItem.Name = null;

            if (cmd.CanExecute(_tdItem))
            {
                cmd.Execute(null);
            }

            Assert.AreEqual("New Label", _tdItem.Name);

            _tdItem.Name = null;

            if (cmd.CanExecute(_tdItem))
            {
                cmd.Execute(null);
            }

            Assert.AreEqual("New Label", _tdItem.Name);
        }

        [Test]
        public void TestAddLabelAboveCommand()
        {
            int count = _tdItem.PeerLabels.Count;
            string name = _tdItem.PeerLabels[0].Name;
            var cmd = _tdItem.AddLabelAboveCommand;

            if (cmd.CanExecute(_tdItem))
            {
                cmd.Execute(null);
            }

            int count2 = _tdItem.PeerLabels.Count;
            string name2 = _tdItem.PeerLabels[0].Name;

            Assert.AreEqual(0, _tdItem.PeerLabels[0].OrderIndex);
            Assert.AreEqual(count + 1, count2);
            Assert.AreNotSame(name, name2);
        }

        [Test]
        public void TestAddLabelBelowCommand()
        {
            int count = _tdItem.PeerLabels.Count;
            string name = _tdItem.PeerLabels[0].Name;
            var cmd = _tdItem.AddLabelAboveCommand;

            if (cmd.CanExecute(_tdItem))
            {
                cmd.Execute(null);
            }

            int count2 = _tdItem.PeerLabels.Count;
            string name2 = _tdItem.PeerLabels[1].Name;

            Assert.AreEqual(0, _tdItem.PeerLabels[0].OrderIndex);
            Assert.AreEqual(1, _tdItem.PeerLabels[1].OrderIndex);
            Assert.AreEqual(count + 1, count2);
            Assert.AreEqual(name, name2);
            Assert.AreEqual("New Label", _tdItem.PeerLabels[0].Name);
        }

        [Test]
        public void TestAddChildLabelCommand()
        {
            int count = _tdItem.SubLabels.Count;
            string name = _tdItem.SubLabels[0].Name;
            var cmd = _tdItem.AddChildLabelCommand;

            if (cmd.CanExecute(_tdItem))
            {
                cmd.Execute(null);
            }

            int count2 = _tdItem.SubLabels.Count;
            string name2 = _tdItem.SubLabels[0].Name;

            Assert.AreEqual(0, _tdItem.SubLabels[0].OrderIndex);
            Assert.AreEqual(1, _tdItem.SubLabels[1].OrderIndex);
            Assert.AreEqual(count + 1, count2);
            Assert.AreNotSame(name, name2);
            Assert.AreEqual("New Label", name2);
        }

        [Test]
        public void TestDeleteLabelCommand()
        {
            int count = _tdItem.PeerLabels.Count;
            string name = _tdItem.PeerLabels[0].Name;
            var cmd = _tdItem.DeleteLabelCommand;
            cmd.Execute(null);

            int count2 = _tdItem.PeerLabels.Count;
            string name2 = _tdItem.PeerLabels[0].Name;

            if (_tdItem.SubLabels != null && _tdItem.SubLabels.Count > 0)
            {
                Assert.AreEqual(0, _tdItem.SubLabels[0].OrderIndex);
                //Assert.AreEqual(1, _tdItem.SubLabels[1].OrderIndex);
                Assert.AreEqual(count, count2);
                Assert.AreEqual(name, name2);
            }
            else
            {
                Assert.AreEqual(0, _tdItem.SubLabels[0].OrderIndex);
                //Assert.AreEqual(1, _tdItem.SubLabels[1].OrderIndex);
                Assert.AreEqual(count - 1, count2);
                Assert.AreNotSame(name, name2);
            }
        }
    }
}